package gui.utils;


import core.para.Para;
import gui.win.initializer.Initializer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;


/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class FilePicker extends JPanel implements ActionListener{
    private JButton file;
    private JTextField txt;
    private boolean open;
     
    public FilePicker(String txt, boolean open, int lenTXT, int lenButton, String msg) {
        super(new FlowLayout());
        this.setLocation(500, 100);
        this.setBorder(BorderFactory.createEmptyBorder(0,-4,0,0));
        this.open = open;
        this.add(getButton(file = new JButton(txt), lenButton, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, txt), BorderLayout.WEST);
        this.txt = new JTextField(lenTXT);
        this.txt.setText(msg);
        this.txt.setBackground(Color.WHITE);
        this.txt.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
        this.txt.setEditable(false);
        this.txt.setFont(Borders.getFont(Borders.GUIFONT));
        this.add(this.txt, BorderLayout.EAST);
    }

    static public int PATH_TO_FOWARD_READS = 0;
    static public int PATH_TO_REVERSE_READS = 1;
    static public int PATH_TO_OUTPUT = 2;
    static public int PATH_TO_LOGFILE = 3;
    private int which = -1;
    public FilePicker(String txt, boolean open, int lenTXT, int lenButton, String msg, int which) {
        super(new FlowLayout());
        this.setLocation(500, 100);
        this.setBorder(BorderFactory.createEmptyBorder(0,-4,0,0));
        this.open = open;
        this.add(getButton(file = new JButton(txt), lenButton, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, txt), BorderLayout.WEST);
        this.txt = new JTextField(lenTXT);
        this.txt.setText(msg);
        this.txt.setBackground(Color.WHITE);
        this.txt.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
        this.txt.setEditable(false);
        this.txt.setFont(Borders.getFont(Borders.GUIFONT));
        this.add(this.txt, BorderLayout.EAST);
        this.which = which;
    }
    public String getPath(){
        if(txt.equals("(optional)")){return "";}
        return txt.getText();
    }

    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(source == file){
            
            try{txt.setText(getFile(new JPanel(), open).getSelectedFile().getPath());}
            catch(Exception e){}
        }
        
        if(which==PATH_TO_FOWARD_READS){Para.PATH_TO_FOWARD_READS = txt.getText();Initializer.setCommand(this.getClass());}
        else if(which==PATH_TO_REVERSE_READS){
            if (!txt.getText().equals(Para.PATH_TO_REVERSE_READS_DEFAULT_VAL)) {
                Para.PATH_TO_REVERSE_READS = txt.getText();
            }
            Initializer.setCommand(this.getClass());
        }
        else if(which==PATH_TO_OUTPUT){Para.PATH_TO_OUTPUT = txt.getText();Initializer.setCommand(this.getClass());}
        else if(which==PATH_TO_LOGFILE){Para.PATH_TO_LOGFILE = txt.getText();Initializer.setCommand(this.getClass());}
    }
    
    private static String CUR_PATH = ".";
    //make cashed version of this so it is not so slow after first loading
    private static final JFileChooser chooser = new JFileChooser();
    
    public static JFileChooser getFile(JPanel menuObj, boolean opening) {
        try {
            chooser.setCurrentDirectory(new File(CUR_PATH));
            int r = 0;
            if (opening) {chooser.showOpenDialog(menuObj);}
            else {r = chooser.showSaveDialog(menuObj);}
            if (r == JFileChooser.APPROVE_OPTION) {
                CUR_PATH = chooser.getSelectedFile().getPath();
                return chooser;
            }
            else {return null;}
        } catch (Exception e){return null;}
    }
    
    public static JPanel getButton(JButton st, int width, int height, Color fCol, Color bCol, ActionListener al, String tooltip){
        st.setForeground(fCol);
        st.setBackground(bCol);
        st.setPreferredSize(new Dimension(width, height));
        st.addActionListener(al);
        st.setToolTipText(tooltip);
        JPanel p = new JPanel(new FlowLayout());
        p.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        p.setOpaque(false);
        p.add(st);
        return p;
    }
}